All right.
In this video, I'm going to continue with my steps.
And in so doing, I'm going to go over return values for functions.
And I'm also going to do a little loop at the end of this to make a whole bunch of steps with very little
code.
I'm going to do a whole video on loops, but I thought a quick introduction would be helpful.
So let's go ahead and stop this, open the workspace and you will see a single script in here.
There's only a single script in the whole game.
I'll put it in the description.
That way, if you don't have it, you just copy it from the description.
And then if you don't have the last video or something like that, you can start from here.
Right?
So I'm going to add, I have this make step function right here where we make a step and then we call
it down here.
Right?
We call it a few times, four times.
And I have a requirement from whoever told me that I got to do this, that we need to get the part outside
of the function and change it in some way, maybe change the material.
Right.
But we can't use the part down here because it's local to our function.
Let's go ahead and fix that.
Let's just say return part.
Now, when we get to the end of this function, we're going to return that part that we created and
we can get it down here.
We could do something like this, we could say local, we call it part, but I think I'm going to call
P one.
Right?
And then we have make step.
Should we get rid of these?
We can add those later.
We'll just work on one call for now.
This is the part, right?
So we call make step.
It does all of its stuff, it returns the part, plops it into p one.
Cool.
Now I can change things pe one material and then we need that enum to assign the material right in m
we have a material section in the E names and then we have diamond plate.
That's a perfect step material, right?
Diamond plate.
Let's go ahead and see if it's actually being changed to diamond plate.
Unfortunately, we're only going to have one step now because I deleted the other ones.
There it is.
And it is, in fact, diamond plated.
Pretty cool.
Let's take a look at it again.
Let's go back steps.
So something you should know.
In Lua you can return more than one value from a function, right?
Like a job, you can only return one thing.
So if you need several things from the function, you have to return a data structure here.
You could just return them separately.
Or I should say individually, I'm going to get the color from the random brick color and return it
as a string as the name of the color.
So I'm going to say local color name, let's say brick color, au part brick color part dot brick color
with a B.
There you go.
Name.
That was a little harder than I thought.
All right.
So this is going to have the name of the brick color.
We're going to copy it.
And then I'll just do a comma and paste color name.
So color name is going to get returned with part.
How do we capture it or how do we just go down here?
Same way we returned it, comma.
Her name so local will apply to both p one and colour name.
Oops I messed up.
PE one control z and colour name.
Let's print this out to make sure that we did in fact get the colour name, print colour name equals
and what I call it colour name.
I was very creative like that colour name.
And you could call this something different, right?
Just so that you know, it doesn't have to match.
Just like I did here with PE one.
You could call the C name, right?
Keep in mind that is a capital N, let's call that C name.
Cool.
Let's go ahead and play it.
Let's get our view output window so we can see it print out.
Maybe I'll move that up a little bit.
Make this a little bigger play.
And where's our step?
There it is.
And it is New Yeller.
Pretty cool.
All right.
We're moving right along.
Let's go ahead and make a whole bunch of steps.
I'm going to get rid of this output window x.
Let's make a whole bunch of steps.
But we can't just keep putting numbers in here.
That's bad programming.
It's called literals.
When you use the actual value, we need to use variables in that way.
We don't have to calculate stuff up without the calculator.
We can let the computer figure out where to put things.
Let's say a local I'm going to call the position of the step, maybe y pause for y position.
Make that zero z pause for z position.
Make that zero.
We're just initializing it to zero.
Now I need a step height h t equals it's going to be two.
Now notice that this is a capital H just like this was a capital N, right?
When we do variables that are multi word we usually or multi word part, we usually capitalize the beginning
of each new word.
It's called camel case.
It's for readability, but in Lua we are case sensitive.
So if you put a lowercase h, you're going to get an error.
Just keep it in mind, right?
Do it exactly the way I'm doing it.
Step distance.
I'll say step dist three.
That's the distance down the Z axis.
All right, so we got these variables.
What are we going to do with them?
Let's come down here.
After we wait 6 seconds to make our step, we've got to get rid of this two in this three.
All right, so we do have a Y value.
We're going to set y a new Y value using the old Y value plus step height.
All right.
So that was zero.
This was a20 plus two equals two.
Right.
And then we're going to do the same with Z.
Oh, these are Y position.
Why?
Pause.
And this is the pause.
There he goes.
Pause.
Plus step distance.
Sweet.
All right, now we can put this in here.
Why?
Pause.
The pause, you're like, Man, that was a lot of extra work just to replace a two and a three.
But let me tell you what's cool about that.
We could do this.
Control C.
Control v.
Let's do it again.
Control v.
And every time this is going to increase.
Hit play.
Let's see what we got.
Three steps.
Now, that is setting us up perfectly for a loop.
For a for loop.
All right.
We can get rid of these because we're going to use a loop.
We don't have to keep pasting code.
That's what we're trying to get away from.
All right.
So how do we do a loop?
Let's go right after this.
Wait 6 seconds.
Now, for loops are a type of loop that are really good.
If you know how many times you want to execute the loop.
I want to make ten steps.
I know that the for loop has to run ten times a while.
Loop, which we'll do in another video, will run an indeterminate period of time and maybe forever.
Right?
So we'll say for or for loop.
We need a loop counter.
I'm going to call it I.
You can call it whatever you want.
I'm going to set it to one.
Then when I gets to ten, I'm going to break the loop and then I'm going to increase I every loop iteration
by one.
Right?
So this is the start, this is the stop and this is the increased value per loop of I.
Q Let's get this control x, control V and then let's print out I.
So I'll do a comma after C name quote I equals put a space there.
I know that we can see what I is doing in the loop.
Let's run it.
Oh, let's see our view output.
There we go.
Look at that.
We got all of our colors.
Here's eye as one, two, three, four, five.
Just like we thought we could go up the steps.
Pretty cool.
So that's your first introduction to Loops?
You know how to do return values from functions.
Next video.
I want to do a little click detector that fires this off and makes steps that would be cool.
I will see you in the next video.